package org.deadline.model.baseapp;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.PrimaryKeyJoinColumn;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.deadline.model.config.ApplicationConfiguration;
import org.deadline.model.config.CustomFormElement;
import org.deadline.model.config.Enumeration;
import org.deadline.model.config.LdapServer;
import org.deadline.model.config.Preference;
import org.deadline.model.config.Role;
import org.deadline.model.config.SecurityQuestion;
import org.deadline.model.config.UIControlConfiguration;

import flexjson.JSONDeserializer;
import flexjson.JSONSerializer;

@Entity
@PrimaryKeyJoinColumn(name="id")
public class Organization extends Principal {

    @NotNull
    @Size(min = 3, max = 100)
    private String name;

    @NotNull
    @Size(min = 3, max = 100)
    private String organizationCode;

    @Size(max = 5000)
    private String description;

    @ManyToOne
    private org.deadline.model.baseapp.Organization parentOrganization;

    @ManyToOne
    private Enumeration organizationType;

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "organization")
    private Set<SecurityQuestion> securityQuestions = new HashSet<SecurityQuestion>();

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "organization")
    private Set<Enumeration> enumerationValues = new HashSet<Enumeration>();

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "organization")
    private Set<Role> roles = new HashSet<Role>();

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "organization")
    private Set<ApplicationConfiguration> applicationConfigurations = new HashSet<ApplicationConfiguration>();

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "organization")
    private Set<Preference> preferences = new HashSet<Preference>();

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "organization")
    private Set<UIControlConfiguration> uiControlConfigurations = new HashSet<UIControlConfiguration>();

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "organization")
    private Set<CustomFormElement> customFormElements = new HashSet<CustomFormElement>();

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "organization")
    private Set<LdapServer> ldapServers = new HashSet<LdapServer>();

	public String toJson() {
        return new JSONSerializer().exclude("*.class").deepSerialize(this);
    }

	public static Organization fromJsonToOrganization(String json) {
        return new JSONDeserializer<Organization>().use(null, Organization.class).deserialize(json);
    }

	public static String toJsonArray(Collection<Organization> collection) {
        return new JSONSerializer().exclude("*.class").deepSerialize(collection);
    }

	public static Collection<Organization> fromJsonArrayToOrganizations(String json) {
        return new JSONDeserializer<List<Organization>>().use(null, ArrayList.class).use("values", Organization.class).deserialize(json);
    }

	public String getName() {
        return this.name;
    }

	public void setName(String name) {
        this.name = name;
    }

	public String getOrganizationCode() {
        return this.organizationCode;
    }

	public void setOrganizationCode(String organizationCode) {
        this.organizationCode = organizationCode;
    }

	public String getDescription() {
        return this.description;
    }

	public void setDescription(String description) {
        this.description = description;
    }

	public Organization getParentOrganization() {
        return this.parentOrganization;
    }

	public void setParentOrganization(Organization parentOrganization) {
        this.parentOrganization = parentOrganization;
    }

	public Enumeration getOrganizationType() {
        return this.organizationType;
    }

	public void setOrganizationType(Enumeration organizationType) {
        this.organizationType = organizationType;
    }

	public Set<SecurityQuestion> getSecurityQuestions() {
        return this.securityQuestions;
    }

	public void setSecurityQuestions(Set<SecurityQuestion> securityQuestions) {
        this.securityQuestions = securityQuestions;
    }

	public Set<Enumeration> getEnumerationValues() {
        return this.enumerationValues;
    }

	public void setEnumerationValues(Set<Enumeration> enumerationValues) {
        this.enumerationValues = enumerationValues;
    }

	public Set<Role> getRoles() {
        return this.roles;
    }

	public void setRoles(Set<Role> roles) {
        this.roles = roles;
    }

	public Set<ApplicationConfiguration> getApplicationConfigurations() {
        return this.applicationConfigurations;
    }

	public void setApplicationConfigurations(Set<ApplicationConfiguration> applicationConfigurations) {
        this.applicationConfigurations = applicationConfigurations;
    }

	public Set<Preference> getPreferences() {
        return this.preferences;
    }

	public void setPreferences(Set<Preference> preferences) {
        this.preferences = preferences;
    }

	public Set<UIControlConfiguration> getUiControlConfigurations() {
        return this.uiControlConfigurations;
    }

	public void setUiControlConfigurations(Set<UIControlConfiguration> uiControlConfigurations) {
        this.uiControlConfigurations = uiControlConfigurations;
    }

	public Set<CustomFormElement> getCustomFormElements() {
        return this.customFormElements;
    }

	public void setCustomFormElements(Set<CustomFormElement> customFormElements) {
        this.customFormElements = customFormElements;
    }

	public Set<LdapServer> getLdapServers() {
        return this.ldapServers;
    }

	public void setLdapServers(Set<LdapServer> ldapServers) {
        this.ldapServers = ldapServers;
    }
	
			
	
	private static final long serialVersionUID = 1L;

	public String toString() {
        return ReflectionToStringBuilder.toString(this, ToStringStyle.SHORT_PREFIX_STYLE);
    }
}
